﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class employee : System.Web.UI.Page
{

    //inject EmployeeBLL
    private IEmployeeBLL empBLL = Factory<IEmployeeBLL>.CreateInstance();

    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(employee).Name);

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadToRepeater();
    }



    #region Private Methods
    private void LoadToRepeater()
    {
        try
        {
            List<Employee> list = empBLL.GetAll();
            if (list != null)
            {
                rptEmployee.DataSource = list;
                rptEmployee.DataBind();

                //hide to msg labels
                lblInform.Visible = false;
                lblMsg.Visible = false;
            }
            else
            {
                addBar.Visible = false;
                rptEmployee.Visible = false;
                lblInform.Text = "Không có nhân viên nào.";
                lblInform.Visible = true;
            }

        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
            lblMsg.Visible = true;
        }
    }

    /// <summary>
    /// Delete
    /// </summary>
    /// <param name="id"></param>
    private void Delete(Guid id)
    {
        try
        {
            //if code haven't used yet
            // if (!empBLL.CheckExistCodeInForeignKey(id))
            // {
            if (empBLL.Delete(id))
                LoadToRepeater();
            // }
            // else //the code is used
            // {
            //  lblMsg.Text = "Không thể xóa!. Nhân viên này đang được sử dụng";
            //  lblMsg.Visible = true;
            // }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            lblMsg.Visible = true;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    private void Active(string command)
    {
        try
        {
            //init BO
            Employee emp = new Employee();
            emp.Id = new Guid(command.Split(';')[1]);
            emp.Active = Boolean.Parse(command.Split(';')[0]);

            if (empBLL.ActiveAction(emp))
                LoadToRepeater();

        }
        catch (Exception ex)
        {
            lblMsg.Visible = true;
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// When press icons
    /// </summary>
    /// <param name="strCommandName"></param>
    /// <param name="strArgument"></param>
    private void Action(string strCommandName, string strArgument)
    {
        switch (strCommandName.ToUpper())
        {
            //press delete button
            case "DELETE":
                Delete(new Guid(strArgument));
                break;
            //press edit icon
            case "EDIT":
                this.Context.Items["id"] = strArgument;
                Server.Transfer(Constants.EMPLOYEE_ACTION);
                break;
            //press active icon
            case "ACTIVE":
                Active(strArgument);
                break;
            default:
                break;
        }
    }
    #endregion


    #region Events
    protected void rptEmployee_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //get Employee from session
        Employee emp = Utils.GetEmployeeSession();

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //find control
            HiddenField hdnButton = (HiddenField)e.Item.FindControl("hdnActive");
            ImageButton btnActive = (ImageButton)e.Item.FindControl("iBtnActive");
            ImageButton btnDelete = (ImageButton)e.Item.FindControl("btnDelete");
            ImageButton btnEdit = (ImageButton)e.Item.FindControl("btnEdit");
            HiddenField hdnId = (HiddenField)e.Item.FindControl("hdnId");

            if (hdnButton.Value.Equals("True"))
            {
                btnActive.ImageUrl = Constants.ACTIVE_IMAGE;
                btnActive.ToolTip = "Click để vô hiệu hóa";
            }

            //can't delete or unactive/active itseft
            if (hdnId.Value.Equals(emp.Id.ToString()))
            {
                btnActive.Visible = false;
                btnDelete.Visible = false;
                btnEdit.Visible = false;
            }
        }

    }
    protected void rptEmployee_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //get command
        string strCommandName = e.CommandName.Trim();
        string strArgument = e.CommandArgument.ToString().Trim();

        Action(strCommandName, strArgument);
    }
    #endregion


}